package com.huobao.yunying.activitys;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.ab.view.slidingmenu.SlidingMenu;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Progect_Ex;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.Goods_list;
import com.huobao.yunying.entity.ProgectList;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品列表
 *
 * @author xrz 类名：Act_Progects_List 时间: 2016年3月17日下午1:48:09
 */
public class Act_Progects_List extends HuobaoBaseActivity implements OnClickListener, OnScrollListener, OnItemClickListener {

	/**
	 * 标题文字
	 */
	private String strTitle;
	private Adapter_Progect_Ex adapter;
	@ViewInject(R.id.listview)
	private ListView listview;
	/**
	 * 1:流通品 2:厂商直批 3:代理品
	 */
	private int type;
	private Http m_Http;
	private List<ProgectList> m_ProgectList;
	/**
	 * 顶部图片
	 */
	private String sPic;

	/**
	 * headview
	 */
	private ImageView m_HeadView;
	@ViewInject(R.id.btn_totop)
	private ImageView m_TotopBtn;


	private SlidingMenu menu;

	@Override
	protected int getContentView() {
		return R.layout.layout_progect_list;
	}

	@Override
	protected String getTitleText() {
		getIntentData();
		return strTitle;
	}


	@Override
	protected void init() {
		m_Http = Http.getInstant();
		mAbTitleBar.setVisibility(View.VISIBLE);
		m_ProgectList = new ArrayList<ProgectList>();
		adapter = new Adapter_Progect_Ex(context, type);
		m_HeadView = new ImageView(context);
		listview.addHeaderView(m_HeadView);
		listview.setOnScrollListener(this);
		listview.setAdapter(adapter);
		m_TotopBtn.setOnClickListener(this);
		getList();
	}

	private void getIntentData() {
		Intent intent = getIntent();
		type = intent.getIntExtra("type", -1);
		strTitle = intent.getStringExtra("title");
	}


	private void getList() {
		Map<String, String> map = null;
		switch (type) {
			case 1:
				map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.ltp);
				break;
			case 2:
				map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.chszp);
				break;
			case 4:
				map = HttpUtil.getParams(HttpConst.FRIST_KEY, "giftpurchase");
				break;
		}
		m_Http.sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				JSONArray array = jsonObject.getJSONArray("goodslist");
				JSONArray sbanber = jsonObject.getJSONArray("sbanber");
				JSONObject object1 = sbanber.getJSONObject(0);
				sPic = object1.getString("spic");
				ImageUtils.loadimg(m_HeadView, sPic, new OnImageLoadListener() {

					@Override
					public void onLoadSuccess(Drawable drawable) {
						int hight = ScreenUtil.getInstant().getDisWidth(context) * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
						LayoutParams params = new LayoutParams(ScreenUtil.getInstant().getDisWidth(context), hight);
						m_HeadView.setLayoutParams(params);
					}
				});
				for (int i = 0; i < array.size(); i++) {
					JSONObject object = array.getJSONObject(i);
					ProgectList progectList = new ProgectList();
					progectList.setCateid(object.getIntValue("cateid"));
					progectList.setCate_name(object.getString("cate_name"));
					progectList.setGoods_list(JSON.parseArray(object.getString("goods_list"), Goods_list.class));
					m_ProgectList.add(progectList);
				}
				adapter.addAll(m_ProgectList);
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_totop:
				listview.smoothScrollToPosition(0);
				break;
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (firstVisibleItem >= 1) {
			m_TotopBtn.setVisibility(View.VISIBLE);
		} else {
			m_TotopBtn.setVisibility(View.GONE);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub

	}
}
